using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using CSW.Framework.Linq.SchemaModel;
using LinqBusinessObjectBuilder.Options;
using LinqBusinessObjectBuilder.ProjectInfo;

namespace LinqBusinessObjectBuilder.Forms
{
    internal partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            Program.ProjectSchemaChanged += Program_ProjectSchemaChanged;

            string lastProjectFileName = GlobalOptions.Instance.CurrentProjectPath;
            if (File.Exists(lastProjectFileName))
                projectOptionsUserControl1.LoadProject(lastProjectFileName);
            else
                projectOptionsUserControl1.LoadProject(new Project());
        }

        private void Program_ProjectSchemaChanged(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();

            Project project = Program.OpenProject;

            TreeNode projectNode = treeView1.Nodes.Add(".project", "Project");
            TreeNode tablesNode = projectNode.Nodes.Add(".tables", "Tables");

            foreach (Table table in project.Schema.Tables.OrderBy(p => p.Schema).ThenBy(p => p.TableName))
            {
                tablesNode.Nodes.Add(table.ToString(), table.ToString());
            }

            treeView1.ExpandAll();
        }

        private void mnuOptions_Click(object sender, EventArgs e)
        {
            Program.ShowGlobalOptionsForm();
        }

        private void mnuGenerate_Click(object sender, EventArgs e)
        {
            projectOptionsUserControl1.Generate();
        }

        private void mnuAbout_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            using (AboutForm form = new AboutForm())
            {
                Cursor.Current = Cursors.Default;
                form.ShowDialog();
            }
        }

        private void mnuOpen_Click(object sender, EventArgs e)
        {
            projectOptionsUserControl1.OpenProject();
        }

        private void mnuSave_Click(object sender, EventArgs e)
        {
            projectOptionsUserControl1.Save();
        }

        private void mnuSaveAs_Click(object sender, EventArgs e)
        {
            projectOptionsUserControl1.SaveAs();
        }

        private void mnuNew_Click(object sender, EventArgs e)
        {
            projectOptionsUserControl1.CreateNewProject();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing || e.CloseReason == CloseReason.None)
            {
                bool cancel;
                projectOptionsUserControl1.AskSaveChanges(out cancel);
                if (cancel)
                    e.Cancel = true;
            }
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            MessageBox.Show(e.Node.ToString());
        }
    }
}
